use tokio;
use tokio::net::UdpSocket;
use tokio::io::AsyncWrite;
// use ctime;
// use rand;

use crate::utils::Except;
use crate::utils::err_with;

use std::rc::Rc;
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::{self,Read, Write};
use std::net::SocketAddr;
use std::time::{Duration, Instant};


#[inline]
fn clock() -> u32 {
    let timespec = ctime::get_time();
    let mills = timespec.sec * 1000 + timespec.nsec as i64 / 1000 / 1000;
    mills as u32
}

pub struct KcpOutput {
    udp: Rc<UdpSocket>,
    peer: SocketAddr,
}

impl KcpOutput {
    async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.udp.send_to(buf, &self.peer).await
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}



// pub async fn udp_connect(remote:&str) -> Except<UdpSocket>{
//     let r: SocketAddr = "127.0.0.1:0".parse().unwrap();
//     Result::Err(err_with("connect failed"))
// }